# Copyright (c) 2024 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

# -*- coding: utf-8 -*-
"""
测试元组方法的内建函数处理
"""

from tests.pytests.helpers.codegen_helpers import compile_chunk, assert_code_contains


def test_tuple_len_method():
    def test_tuple() -> int:
        t1 = (1, 2, 3)
        t2: tuple[int, str, bool] = (1, "hello", True)
        t3: tuple = (1, "hello", True)
        t4: tuple[()] = ()
        a = t1[len(t1) + 1]
        b = t1[-1]
        return len(t1) + len(t2) + len(t3) + len(t4)

    cpp = compile_chunk(test_tuple)
    expected = """
    int test_tuple() {
        std::tuple<int, int, int> t1 = std::make_tuple(1, 2, 3);
        std::tuple<int, std::string, bool> t2 = std::make_tuple(1, "hello", true);
        mc::variants t3 = mc::variants{1, "hello", true};
        std::tuple<> t4 = std::tuple<>{};
        int a = mcpy::tuple_getitem<int>(t1, mcpy::tuple_len(t1) + 1);
        int b = mcpy::tuple_getitem<int>(t1, -1);
        return mcpy::tuple_len(t1) + mcpy::tuple_len(t2) + t3.size() + mcpy::tuple_len(t4);
    }
    """
    assert_code_contains(cpp, expected)
